//package com.yulian.web_api.supplier_management;
//
//import com.healthmarketscience.sqlbuilder.CustomSql;
//import com.healthmarketscience.sqlbuilder.SelectQuery;
//import com.yulian.repository.CommonRepositoryImpl;
//import com.yulian.util.CommonUtils;
//import io.netty.util.internal.logging.InternalLogger;
//import io.netty.util.internal.logging.Log4J2LoggerFactory;
//import io.vertx.core.CompositeFuture;
//import io.vertx.core.Future;
//import io.vertx.core.Promise;
//import io.vertx.core.json.JsonArray;
//import io.vertx.core.json.JsonObject;
//import io.vertx.ext.auth.User;
//import io.vertx.pgclient.PgPool;
//import io.vertx.sqlclient.SqlConnection;
//import io.vertx.sqlclient.Tuple;
////import lombok.extern.slf4j.Slf4j;
//import lombok.extern.slf4j.Slf4j;
//
//import java.util.*;
//
//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;
//
///**
// * 该类位于业务逻辑层，主要实现供应商提货计划支付明细单数据的查询、编辑的功能，为MainVerticle中的REST接口提供服务
// *
// * @author Xdm
// */
//@Slf4j
//public class SupplierPickupPlanPaymentDetailManager {
//	//private  final Logger log =   LogManager.getLogger(SupplierPickupPlanPaymentDetailManager.class);
////    final InternalLogger log = Log4J2LoggerFactory.getInstance(SupplierPickupPlanPaymentDetailManager.class);
//	//private final Logger log = LoggerFactory.getLogger(SupplierPickupPlanPaymentDetailManager.class);
//    CommonRepositoryImpl commonRepository = new CommonRepositoryImpl();
//
//    /**
//     * 创建查询符合搜索条件记录的sql语句
//     *
//     * @param queryJS      查询条件
//     * @param query_limit  页面记录数
//     * @param query_offset 页面偏移
//     * @return 查询符合搜索条件的sql语句
//     */
//    public String createSelectSql(JsonObject queryJS, int query_limit, int query_offset) {
//        SelectQuery sq = new SelectQuery();
//        sq.addCustomColumns(new CustomSql("id"), new CustomSql("prepare_pickup_plan_price_bid_id"), new CustomSql("supplier_pickup_plan_detail_id"),
//        new CustomSql("supplier_pickup_plan_payment_id"), new CustomSql("aggregation_id"), new CustomSql("supply_chain_goods_id"),
//        new CustomSql("real_settlement_package_spec"), new CustomSql("real_material_price"), new CustomSql("real_cost"),
//        new CustomSql("settlement_price"), new CustomSql("payment_weight"), new CustomSql("payment_total_weight"),
//        new CustomSql("real_payment_amount"), new CustomSql("real_payment_total_amount"), new CustomSql("real_pickup_number"),
//        new CustomSql("real_pickup_total_number"), new CustomSql("is_delete"), new CustomSql("create_user"),
//        new CustomSql("update_user"), new CustomSql("create_time"), new CustomSql("update_time")).addCustomFromTable("supplier_pickup_plan_payment_detail");
//        this.commonRepository.processQueryJsonObjectIsDelete0(sq, queryJS);
//        String final_sql = sq.validate() + " order by update_time desc limit " + query_limit + " offset " + query_offset;
//        log.info("final_sql,{}",final_sql);
//        return final_sql;
//    }
//
//    /**
//     * 构造查询符合条件求和的SQL语句
//     *
//     * @param queryJS 查询条件
//     * @return 查询符合条件求和的SQL语句
//     */
//    public String createSelectSumSql(JsonObject queryJS) {
//        SelectQuery sq = new SelectQuery();
//        sq.addCustomColumns(new CustomSql("sum(payment_weight) as payment_total_weight"),
//        new CustomSql("sum(real_payment_amount) as real_payment_total_amount"),
//        new CustomSql("sum(real_pickup_number) as real_pickup_total_number")).addCustomFromTable("supplier_pickup_plan_payment_detail");
//        this.commonRepository.processQueryJsonObjectIsDelete0(sq, queryJS);
//        return sq.validate().toString();
//    }
//
//    /**
//     * 构造查询符合条件的记录数的SQL语句
//     *
//     * @param queryJS 查询条件
//     * @return 查询符合条件的记录数的SQL语句
//     */
//    public String createSelectCountSql(JsonObject queryJS) {
//        SelectQuery sq = new SelectQuery();
//        sq.addCustomColumns(new CustomSql("count(*) as total")).addCustomFromTable("supplier_pickup_plan_payment_detail");
//        this.commonRepository.processQueryJsonObjectIsDelete0(sq, queryJS);
//        return sq.validate().toString();
//    }
//
//    /**
//     * 根据传入的查询参数获取满足条件的记录
//     *
//     * @param client    连接池对象
//     * @param config    配置对象
//     * @param bodyJSObj body参数对象
//     * @return 满足条件的记录
//     */
//    public Future<JsonObject> getRecords(PgPool client, JsonObject config, JsonObject bodyJSObj) {
//        Promise<JsonObject> promise = Promise.promise();
//        // 用于传递给构建sql的JsonObject
//        JsonObject queryJSObj = new JsonObject();
//        // 验证数值字段
//        List<String> numeric_fields = Arrays.asList("page_size", "page_index");
//        JsonObject numeric = this.commonRepository.verifyAllParamNumeric(bodyJSObj, numeric_fields);
//        if (numeric != null) {
//            promise.complete(numeric);
//            return promise.future();
//        }
//        int page_size = bodyJSObj.getInteger("page_size");
//        int page_index = bodyJSObj.getInteger("page_index");
//        // 计算页面偏移
//        int query_offset = (page_index - 1) * page_size;
//        // 判断查询方式（根据总表的一条记录查询或根据筛选框查询或根据本表的id查询）
//        if (bodyJSObj.containsKey("supplier_pickup_plan_payment_detail_id")) {
//            // 验证数值字段
//            List<String> numeric_fields2 = List.of("supplier_pickup_plan_payment_detail_id");
//            JsonObject numeric2 = this.commonRepository.verifyAllParamNumeric(bodyJSObj, numeric_fields2);
//            if (numeric2 != null) {
//                promise.complete(numeric2);
//                return promise.future();
//            }
//            // 构建sql的JsonObject
//            queryJSObj.put("id", bodyJSObj.getLong("supplier_pickup_plan_payment_detail_id"));
//        } else if (bodyJSObj.containsKey("supplier_pickup_plan_payment_id")) {
//            // 验证数值字段
//            List<String> numeric_fields2 = List.of("supplier_pickup_plan_payment_id");
//            JsonObject numeric2 = this.commonRepository.verifyAllParamNumeric(bodyJSObj, numeric_fields2);
//            if (numeric2 != null) {
//                promise.complete(numeric2);
//                return promise.future();
//            }
//            // 构建sql的JsonObject
//            queryJSObj.put("supplier_pickup_plan_payment_id", bodyJSObj.getLong("supplier_pickup_plan_payment_id"));
//        } else {
//            // 验证字符串字段
//            List<String> must_fields = Arrays.asList("supplier_pickup_plan_payment_id_list", "aggregation_id_list", "supply_chain_goods_id_list");
//            JsonObject must = this.commonRepository.verifyAllParamMust(bodyJSObj, must_fields);
//            if (must != null) {
//                promise.complete(must);
//                return promise.future();
//            }
//            // 获取bodyJSObj参数对象中字段的值, 各字段注释请参看数据库设计文档中数据表supplier_pickup_plan_payment_detail的说明
//            String supplier_pickup_plan_payment_id_list = bodyJSObj.getString("supplier_pickup_plan_payment_id_list");
//            String aggregation_id_list = bodyJSObj.getString("aggregation_id_list");
//            String supply_chain_goods_id_list = bodyJSObj.getString("supply_chain_goods_id_list");
//            // 用于传递给构建sql的JsonObject
//            queryJSObj.put("supplier_pickup_plan_payment_id", supplier_pickup_plan_payment_id_list);
//            queryJSObj.put("aggregation_id", aggregation_id_list);
//            queryJSObj.put("supply_chain_goods_id", supply_chain_goods_id_list);
//            //queryJSObj.put("is_delete", "[0]");
//        }
//        // 定义多个set，存储每一个supplier_pickup_plan_payment_detail中多个id
//        Set<String> aggregation_id_list_set = new HashSet<>();
//        Set<String> supply_chain_goods_id_list_set = new HashSet<>();
//        Set<String> supplier_pickup_plan_payment_id_list_set = new HashSet<>();
//        Set<String> prepare_pickup_plan_price_bid_id_list_set = new HashSet<>();
//        Set<String> supplier_pickup_plan_detail_id_list_set = new HashSet<>();
//        // 连接数据库
//        this.commonRepository.getCon(client).onSuccess(conn -> {
//            // 查询记录
//            String select_records_sql = this.createSelectSql(queryJSObj, page_size, query_offset);
//            Future<JsonArray> getSupplierPickupPlanPaymentDetailRecordsFuture = this.commonRepository.queryJsonArray(conn, select_records_sql, "get_supplier_pickup_plan_payment_detail_records");
//            // 求和
//            String select_sum_sql = this.createSelectSumSql(queryJSObj);
//            Future<JsonObject> getSupplierPickupPlanPaymentDetailSumFuture = this.commonRepository.querySumJsonObject(conn, select_sum_sql, "get_supplier_pickup_plan_payment_detail_sum");
//            // 并发执行多个Future
//            CompositeFuture.join(Arrays.asList(getSupplierPickupPlanPaymentDetailRecordsFuture, getSupplierPickupPlanPaymentDetailSumFuture)).onComplete(ar1 -> {
//                if (ar1.succeeded()) {
//                    // 获取Future执行的结果
//                    JsonArray resJSArray = (JsonArray) ar1.result().list().get(0);
//                    JsonObject sumJSObject = (JsonObject) ar1.result().list().get(1);
//                    // 最后返回的JsonObject对象
//                    JsonObject responseJSObj = new JsonObject();
//                    // 最终返还的记录数和记录
//                    JsonObject dataJSObj = new JsonObject();
//                    if (!resJSArray.isEmpty()) {
//                        // 取出每条记录中含code字段的值放入对应的集合
//                        for (int i = 0; i < resJSArray.size(); i++) {
//                            JsonObject jsonObj = resJSArray.getJsonObject(i);
//                            aggregation_id_list_set.add(jsonObj.getString("aggregation_id"));
//                            supply_chain_goods_id_list_set.add(jsonObj.getString("supply_chain_goods_id"));
//                            supplier_pickup_plan_payment_id_list_set.add(jsonObj.getString("supplier_pickup_plan_payment_id"));
//                            prepare_pickup_plan_price_bid_id_list_set.add(jsonObj.getString("prepare_pickup_plan_price_bid_id"));
//                            supplier_pickup_plan_detail_id_list_set.add(jsonObj.getString("supplier_pickup_plan_detail_id"));
//                        }
//                        // 获取满足条件的记录总数
//                        String select_count_sql = this.createSelectCountSql(queryJSObj);
//                        Future<Integer> getTotalFuture = this.commonRepository.getRecordsCount(conn, select_count_sql, "get_supplier_pickup_plan_payment_detail_count");
//                        // 获取聚合商品id和name
//                        String aggregation_sql = "select distinct id, aggregation_name from aggregation_goods where id = any(array[" + String.join(",", aggregation_id_list_set) + "])";
//                        Future<JsonObject> getAggregationNameFuture = this.commonRepository.getCodeNamePairJsonObject(conn, aggregation_sql, "get_aggregation_name");
//                        // 获取供应链商品id和name
//                        String supply_chain_goods_sql = "select distinct id, supply_chain_goods_name from supply_chain_goods where id = any(array[" + String.join(",", supply_chain_goods_id_list_set) + "])";
//                        Future<JsonObject> getSupplyChainGoodsNameFuture = this.commonRepository.getCodeNamePairJsonObject(conn, supply_chain_goods_sql, "get_supply_chain_goods_name");
//                        // 获取供应商提货计划支付单的id和code
//                        String supplier_pickup_plan_payment_sql = "select distinct id, supplier_pickup_plan_payment_code from supplier_pickup_plan_payment where id = any(array[" + String.join(",", supplier_pickup_plan_payment_id_list_set) + "])";
//                        Future<JsonObject> getSupplierPickupPlanPaymentCodeFuture = this.commonRepository.getCodeNamePairJsonObject(conn, supplier_pickup_plan_payment_sql, "get_supplier_pickup_plan_payment_code");
//                        // 获取比价单的数据
//                        String prepare_pickup_plan_price_bid_sql = "select distinct id, settlement_type_id, supply_chain_goods_grade_id, prepare_pickup_plan_weight, settlement_package_spec, material_cost, cost_per_jin " +
//                        "from prepare_pickup_plan_price_bid where id = any(array[" + String.join(",", prepare_pickup_plan_price_bid_id_list_set) + "])";
//                        Future<JsonObject> getPreparePickupPlanPriceBidFuture = this.commonRepository.queryJsonArrayManyToMany(conn, prepare_pickup_plan_price_bid_sql, "get_prepare_pickup_plan_price_bid");
//                        // 获取提货计划明细单的数据
//                        String supplier_pickup_plan_detail_sql = "select distinct id, correct_pickup_plan_weight, pickup_plan_number, settlement_amount " +
//                        "from supplier_pickup_plan_detail where id = any(array[" + String.join(",", supplier_pickup_plan_detail_id_list_set) + "])";
//                        Future<JsonObject> getSupplierPickupPlanDetailFuture = this.commonRepository.queryJsonArrayManyToMany(conn, supplier_pickup_plan_detail_sql, "get_supplier_pickup_plan_detail");
//                        // 并发执行多个Future
//                        CompositeFuture.join(Arrays.asList(getTotalFuture, getAggregationNameFuture, getSupplyChainGoodsNameFuture,
//                        getSupplierPickupPlanPaymentCodeFuture, getPreparePickupPlanPriceBidFuture, getSupplierPickupPlanDetailFuture)).onComplete(ar2 -> {
//                            if (ar2.succeeded()) {
//                                // 获取Future执行的结果
//                                int total_records = (int) ar2.result().list().get(0);
//                                JsonObject aggregation_info = (JsonObject) ar2.result().list().get(1);
//                                JsonObject supply_chain_goods_info = (JsonObject) ar2.result().list().get(2);
//                                JsonObject supplier_pickup_plan_payment_info = (JsonObject) ar2.result().list().get(3);
//                                JsonObject prepare_pickup_plan_price_bid_info = (JsonObject) ar2.result().list().get(4);
//                                JsonObject supplier_pickup_plan_detail_info = (JsonObject) ar2.result().list().get(5);
//                                Set<String> settlement_type_id_list_set = new HashSet<>();
//                                Set<String> goods_grade_id_list_set = new HashSet<>();
//                                prepare_pickup_plan_price_bid_info.forEach(item -> {
//                                    JsonObject value = (JsonObject) item.getValue();
//                                    settlement_type_id_list_set.add(value.getString("settlement_type_id"));
//                                    goods_grade_id_list_set.add(value.getString("supply_chain_goods_grade_id"));
//                                });
//                                // 获取结算类型id和name
//                                String settlement_type_sql = "select distinct id, type_name from settlement_type where id = any(array[" + String.join(",", settlement_type_id_list_set) + "])";
//                                Future<JsonObject> getSettlementTypeNameFuture = this.commonRepository.getCodeNamePairJsonObject(conn, settlement_type_sql, "get_settlement_type_name");
//                                // 获取商品等级id和name
//                                String goods_grade_sql = "select distinct id, grade_name from goods_grade where id = any(array[" + String.join(",", goods_grade_id_list_set) + "])";
//                                Future<JsonObject> getGoodsGradeNameFuture = this.commonRepository.getCodeNamePairJsonObject(conn, goods_grade_sql, "get_goods_grade_name");
//                                CompositeFuture.join(Arrays.asList(getSettlementTypeNameFuture, getGoodsGradeNameFuture)).onComplete(ar3 -> {
//                                    if (ar3.succeeded()) {
//                                        // 获取Future执行的结果
//                                        JsonObject settlement_type_info = (JsonObject) ar3.result().list().get(0);
//                                        JsonObject goods_grade_info = (JsonObject) ar3.result().list().get(1);
//                                        // 从配置文件中读取
//                                        JsonObject is_delete_info = config.getJsonObject("DELETE_INFO");
//                                        // 最后组装含有name的Json对象和数组
//                                        JsonArray recordsJSArray = new JsonArray();
//                                        for (int i = 0; i < resJSArray.size(); i++) {
//                                            JsonObject record_obj = resJSArray.getJsonObject(i);
//                                            // 获取比价单数据
//                                            JsonObject prepare_pickup_plan_price_bid = prepare_pickup_plan_price_bid_info.getJsonObject(record_obj.getString("prepare_pickup_plan_price_bid_id"));
//                                            record_obj.put("settlement_type_id", prepare_pickup_plan_price_bid.getString("settlement_type_id"));
//                                            record_obj.put("settlement_type_name", settlement_type_info.getString(record_obj.getString("settlement_type_id")));
//                                            record_obj.put("supply_chain_goods_grade_id", prepare_pickup_plan_price_bid.getString("supply_chain_goods_grade_id"));
//                                            record_obj.put("supply_chain_goods_grade_name", goods_grade_info.getString(record_obj.getString("supply_chain_goods_grade_id")));
//                                            record_obj.put("prepare_pickup_plan_weight", prepare_pickup_plan_price_bid.getString("prepare_pickup_plan_weight"));
//                                            record_obj.put("settlement_package_spec", prepare_pickup_plan_price_bid.getString("settlement_package_spec"));
//                                            record_obj.put("material_cost", prepare_pickup_plan_price_bid.getString("material_cost"));
//                                            record_obj.put("cost_per_jin", prepare_pickup_plan_price_bid.getString("cost_per_jin"));
//                                            // 获取提货计划明细单数据
//                                            JsonObject supplier_pickup_plan_detail = supplier_pickup_plan_detail_info.getJsonObject(record_obj.getString("supplier_pickup_plan_detail_id"));
//                                            record_obj.put("correct_pickup_plan_weight", supplier_pickup_plan_detail.getString("correct_pickup_plan_weight"));
//                                            record_obj.put("pickup_plan_number", supplier_pickup_plan_detail.getString("pickup_plan_number"));
//                                            record_obj.put("settlement_amount", supplier_pickup_plan_detail.getString("settlement_amount"));
//                                            // 获取aggregation_name
//                                            record_obj.put("aggregation_name", aggregation_info.getString(record_obj.getString("aggregation_id")));
//                                            // 获取supply_chain_goods_name
//                                            record_obj.put("supply_chain_goods_name", supply_chain_goods_info.getString(record_obj.getString("supply_chain_goods_id")));
//                                            // 获取supplier_pickup_plan_payment_code
//                                            record_obj.put("supplier_pickup_plan_payment_code", supplier_pickup_plan_payment_info.getString(record_obj.getString("supplier_pickup_plan_payment_id")));
//                                            // 从配置文件中获取name
//                                            record_obj.put("is_delete_name", is_delete_info.getString(record_obj.getString("is_delete")));
//                                            recordsJSArray.add(record_obj);
//                                        }
//                                        dataJSObj.put("total", total_records).put("records", recordsJSArray);
//                                        dataJSObj.put("payment_total_weight", sumJSObject.getDouble("payment_total_weight"));
//                                        dataJSObj.put("real_payment_total_amount", sumJSObject.getDouble("real_payment_total_amount"));
//                                        dataJSObj.put("real_pickup_total_number", sumJSObject.getInteger("real_pickup_total_number"));
//                                        responseJSObj.put("code", 200).put("msg", "获取记录成功").put("data", dataJSObj);
//                                        promise.complete(responseJSObj);
//                                    } else {
//                                        log.info("获取记录数据发生错误: {}", ar3.cause().getMessage());
//                                        promise.complete(CommonUtils.createExceptionJsonObject(ar3, "get_supplier_pickup_plan_payment_detail_records", "获取商品等级或结算类型失败"));
//                                    }
//                                });
//                            } else {
//                                log.info("获取记录数据发生错误: {}", ar2.cause().getMessage());
//                                promise.complete(CommonUtils.createExceptionJsonObject(ar2, "get_supplier_pickup_plan_payment_detail_records", "获取供应商提货计划支付明细单失败"));
//                            }
//                        });
//                    } else {
//                        dataJSObj.put("total", 0).put("records", resJSArray);
//                        responseJSObj.put("code", 200).put("msg", "没有满足条件的数据").put("data", dataJSObj);
//                        promise.complete(responseJSObj);
//                    }
//                } else {
//                    log.info("获取记录数据发生错误: {}", ar1.cause().getMessage());
//                    promise.fail(CommonUtils.createExceptionJsonObjectStr(ar1, "get_supplier_pickup_plan_payment_detail_records", "查询数据发生错误"));
//                }
//            });
//        }).onFailure(err -> promise.fail(CommonUtils.createJsonObject(err, "get_supplier_pickup_plan_payment_detail_records", "数据库连接失败")));
//        return promise.future();
//    }
//
//    /**
//     * 该方法实现接收web前端通过body传入的参数，调用通用类CommonRepositoryImpl的方法实现数据的增、改。
//     * 通过含参数的Json对象bodyJSObj，通过method字段获取操作类型，分别是insert，update。
//     * 1) 操作类型为 insert，新增数据
//     * 2) 操作类型为 update，根据id更新记录
//     *
//     * @param client    连接池对象
//     * @param bodyJSObj body参数对象
//     * @param user      操作用户
//     * @return 操作的结果
//     */
//    public Future<JsonObject> insertOrUpdate(PgPool client, JsonObject bodyJSObj, User user) {
//        Promise<JsonObject> promise = Promise.promise();
//        int user_id = user.attributes().getJsonObject("accessToken").getInteger("userId");
//        // 验证数值字段
//        List<String> numeric_fields = Arrays.asList("prepare_pickup_plan_price_bid_id", "supplier_pickup_plan_detail_id", "supplier_pickup_plan_payment_id","aggregation_id","supply_chain_goods_id");
//        JsonObject numeric = this.commonRepository.verifyAllParamNumeric(bodyJSObj, numeric_fields);
//        if (numeric != null) {
//            promise.complete(numeric);
//            return promise.future();
//        }
//        // 验证字符串字段
//        List<String> must_fields = List.of("payment_detail_list");
//        JsonObject must = this.commonRepository.verifyAllParamMust(bodyJSObj, must_fields);
//        if (must != null) {
//            promise.complete(must);
//            return promise.future();
//        }
//        // 获取bodyJSObj参数对象中字段的值, 各字段注释请参看数据库设计文档中数据表supplier_pickup_plan_payment_detail的说明
//        long prepare_pickup_plan_price_bid_id = bodyJSObj.getLong("prepare_pickup_plan_price_bid_id");
//        long supplier_pickup_plan_detail_id = bodyJSObj.getLong("supplier_pickup_plan_detail_id");
//        long supplier_pickup_plan_payment_id = bodyJSObj.getLong("supplier_pickup_plan_payment_id");
//        long aggregation_id = bodyJSObj.getLong("aggregation_id");
//        long supply_chain_goods_id = bodyJSObj.getLong("supply_chain_goods_id");
//        JsonArray payment_detail_list = bodyJSObj.getJsonArray("payment_detail_list");
//        // 定义一个待更新的batch
//        List<Tuple> update_supplier_pickup_plan_payment_detail_batch = new ArrayList<>(1);
//        // 定义一个待拆分的batch
//        List<Tuple> insert_supplier_pickup_plan_payment_detail_batch = new ArrayList<>(payment_detail_list.size() - 1);
//        for (int i = 0; i < payment_detail_list.size(); i++) {
//            JsonObject json = payment_detail_list.getJsonObject(i);
//            // 验证数值字段
//            List<String> numeric_fields2 = Arrays.asList("id", "real_settlement_package_spec", "real_pickup_number", "real_material_price",
//            "real_cost", "settlement_price", "payment_weight", "real_payment_amount");
//            JsonObject numeric2 = this.commonRepository.verifyAllParamNumeric(json, numeric_fields2);
//            if (numeric2 != null) {
//                promise.complete(numeric2);
//                return promise.future();
//            }
//            long id = json.getLong("id");
//            double real_settlement_package_spec = json.getDouble("real_settlement_package_spec");
//            int real_pickup_number = json.getInteger("real_pickup_number");
//            double real_material_price = json.getDouble("real_material_price");
//            double real_cost = json.getDouble("real_cost");
//            double settlement_price = json.getDouble("settlement_price");
//            double payment_weight = json.getDouble("payment_weight");
//            double real_payment_amount = json.getDouble("real_payment_amount");
//            if (id == 0) {
//                insert_supplier_pickup_plan_payment_detail_batch.add(Tuple.of(prepare_pickup_plan_price_bid_id, supplier_pickup_plan_detail_id,
//                supplier_pickup_plan_payment_id,aggregation_id,supply_chain_goods_id, real_settlement_package_spec, real_pickup_number, real_material_price,
//                real_cost, settlement_price, payment_weight, real_payment_amount, user_id));
//            } else {
//                update_supplier_pickup_plan_payment_detail_batch.add(Tuple.of(real_settlement_package_spec, real_pickup_number,
//                real_material_price, real_cost, settlement_price, payment_weight, real_payment_amount, user_id, id));
//            }
//        }
//        // 连接数据库
//        this.commonRepository.getCon(client).onSuccess(conn -> this.updateSupplierPickupPlanPaymentDetailTransaction(conn,
//        supplier_pickup_plan_detail_id, supplier_pickup_plan_payment_id, user_id, update_supplier_pickup_plan_payment_detail_batch,
//        insert_supplier_pickup_plan_payment_detail_batch)
//        .onSuccess(resultJSObj -> {
//            // 写入操作日志
//            this.commonRepository.writePlatformLog(conn, user_id, "/web/supplier_pickup_plan_payment_detail/update", bodyJSObj, resultJSObj, "")
//            .onComplete(ar3 -> promise.complete(resultJSObj));
//        }).onFailure(err -> promise.fail(CommonUtils.createExceptionJsonObjectStr(err, "update_supplier_pickup_plan_payment_detail", "更新供应商提货计划支付明细单失败"))))
//        .onFailure(err -> promise.fail(CommonUtils.createExceptionJsonObjectStr(err, "update_supplier_pickup_plan_payment_detail", "数据库连接失败")));
//        return promise.future();
//    }
//
//    /**
//     * 此方法用于处理拆分供应商提货计划支付明细单后的事务
//     *
//     * @param conn                                             数据库连接对象
//     * @param supplier_pickup_plan_detail_id                   供应商提货计划明细单id
//     * @param supplier_pickup_plan_payment_id                  供应商提货计划支付单id
//     * @param user_id                                          用户id
//     * @param update_supplier_pickup_plan_payment_detail_batch 更新供应商提货计划支付明细单的batch
//     * @param insert_supplier_pickup_plan_payment_detail_batch 插入到供应商提货计划支付明细单的batch
//     * @return 处理的结果
//     */
//    public Future<JsonObject> updateSupplierPickupPlanPaymentDetailTransaction(SqlConnection conn, long supplier_pickup_plan_detail_id,
//                                                                               long supplier_pickup_plan_payment_id, int user_id,
//                                                                               List<Tuple> update_supplier_pickup_plan_payment_detail_batch,
//                                                                               List<Tuple> insert_supplier_pickup_plan_payment_detail_batch) {
//        Promise<JsonObject> promise = Promise.promise();
//        JsonObject responseJSObj = new JsonObject();
//        responseJSObj.put("code", 200).put("msg", "更新供应商提货计划支付明细单: ");
//        double[] payment_total_weight = new double[1];
//        double[] real_pickup_total_number = new double[1];
//        double[] real_payment_total_amount = new double[1];
//        // 构建SQL语句
//        String update_sql1 = "update supplier_pickup_plan_payment_detail set real_settlement_package_spec = $1, real_pickup_number = $2, " +
//        "real_material_price = $3, real_cost = $4, settlement_price = $5, payment_weight = $6, real_payment_amount = $7, update_user = $8 where id = $9 returning id";
//        String insert_sql = "insert into supplier_pickup_plan_payment_detail(prepare_pickup_plan_price_bid_id, supplier_pickup_plan_detail_id, " +
//        "supplier_pickup_plan_payment_id, aggregation_id,supply_chain_goods_id,real_settlement_package_spec, real_pickup_number, real_material_price, real_cost, " +
//        "settlement_price, payment_weight, real_payment_amount, create_user) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) returning id";
//        String select_sum_sql = "select sum(payment_weight) as payment_total_weight, sum(real_pickup_number) as real_pickup_total_number, " +
//        "sum(real_payment_amount) as real_payment_total_amount from supplier_pickup_plan_payment_detail where supplier_pickup_plan_detail_id = $1";
//        String update_sql2 = "update supplier_pickup_plan_payment_detail set payment_total_weight = $1, real_pickup_total_number = $2, " +
//        "real_payment_total_amount = $3, update_user = $4 where supplier_pickup_plan_detail_id = $5 returning id";
//        String update_sql3 = "update supplier_pickup_plan_payment set payment_total_amount = (select sum(real_payment_amount) " +
//        "from supplier_pickup_plan_payment_detail where supplier_pickup_plan_payment_id = $1), update_user = $2 where id = $3 returning id";
//        if(insert_supplier_pickup_plan_payment_detail_batch.size()>0) {
//
//        	// 事务开始
//            conn.begin()
//            .compose(res1 -> conn.preparedQuery(update_sql1).executeBatch(update_supplier_pickup_plan_payment_detail_batch).onComplete(ar1 -> {
//                if (ar1.succeeded()) {
//                    if (ar1.result().rowCount() > 0) {
//                        // 取出记录的id
//                        long[] temp_id = new long[1];
//                        ar1.result().forEach(item -> temp_id[0] = item.getLong("id"));
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "更新供应商提货计划支付明细单成功, 记录id: " + temp_id[0]);
//                    } else { // 返回结果行数为0
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "没有更新供应商提货计划支付明细单");
//                    }
//                    log.info("更新供应商提货计划支付明细单成功, 更新行数: " + ar1.result().rowCount());
//                } else {
//                    log.info("更新供应商提货计划支付明细单失败, 失败原因: " + ar1.cause().getMessage());
//                    promise.fail(CommonUtils.createExceptionJsonObjectStr(ar1, "/web/supplier_pickup_plan_payment_detail/update", "服务器处理异常", update_sql1));
//                }
//            })
//            .compose(res2 ->  conn.preparedQuery(insert_sql).executeBatch(insert_supplier_pickup_plan_payment_detail_batch).onComplete(ar2 -> {
//                if (ar2.succeeded()) {
//                    if (ar2.result().rowCount() > 0) {
//                        // 取出新增记录的id
//                        List<Long> ids = new ArrayList<>(ar2.result().rowCount());
//                        ar2.result().forEach(item -> ids.add(item.getLong("id")));
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; 更新供应商提货计划支付明细单成功, 记录id: " + ids);
//                    } else { // 返回结果行数为0
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; 没有更新供应商提货计划支付明细单");
//                    }
//                    log.info("更新供应商提货计划支付明细单成功, 更新行数: " + ar2.result().rowCount());
//                } else {
//                    log.info("更新供应商提货计划支付明细单失败, 失败原因: " + ar2.cause().getMessage());
//                    promise.fail(CommonUtils.createExceptionJsonObjectStr(ar2, "/web/supplier_pickup_plan_payment_detail/update", "服务器处理异常", insert_sql));
//                }
//              })
//            .compose(res3 -> conn.preparedQuery(select_sum_sql).execute(Tuple.of(supplier_pickup_plan_detail_id)).onComplete(ar3 -> {
//                if (ar3.succeeded()) {
//                    ar3.result().forEach(item -> {
//                        payment_total_weight[0] = item.getDouble("payment_total_weight");
//                        real_pickup_total_number[0] = item.getDouble("real_pickup_total_number");
//                        real_payment_total_amount[0] = item.getDouble("real_payment_total_amount");
//                    });
//                    log.info("供应商提货计划支付明细单求和成功");
//                } else {
//                    log.info("供应商提货计划支付明细单求和失败, 失败原因: " + ar3.cause().getMessage());
//                    promise.fail(CommonUtils.createExceptionJsonObjectStr(ar3, "/web/supplier_pickup_plan_payment_detail/update", "服务器处理异常", select_sum_sql));
//                }
//            })
//            .compose(res4 -> conn.preparedQuery(update_sql2).execute(Tuple.of(payment_total_weight[0], real_pickup_total_number[0],
//            real_payment_total_amount[0], user_id, supplier_pickup_plan_detail_id)).onComplete(ar4 -> {
//                if (ar4.succeeded()) {
//                    if (ar4.result().rowCount() > 0) {
//                        // 取出新增记录的id
//                        List<Long> ids = new ArrayList<>(ar4.result().rowCount());
//                        ar4.result().forEach(item -> ids.add(item.getLong("id")));
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; 新增供应商提货计划支付明细单成功, 记录id: " + ids);
//                    } else { // 返回结果行数为0
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; 没有新增供应商提货计划支付明细单");
//                    }
//                    log.info("新增供应商提货计划支付明细单成功, 新增行数: " + ar4.result().rowCount());
//                } else {
//                    log.info("新增供应商提货计划支付明细单失败, 失败原因: " + ar4.cause().getMessage());
//                    promise.fail(CommonUtils.createExceptionJsonObjectStr(ar4, "/web/supplier_pickup_plan_payment_detail/update", "服务器处理异常", update_sql2));
//                }
//            })
//            .compose(res5 -> conn.preparedQuery(update_sql3).execute(Tuple.of(supplier_pickup_plan_payment_id, user_id, supplier_pickup_plan_payment_id)).onComplete(ar4 -> {
//                if (ar4.succeeded()) {
//                    if (ar4.result().rowCount() > 0) {
//                        // 取出记录的id
//                        long[] temp_id = new long[1];
//                        ar4.result().forEach(item -> temp_id[0] = item.getLong("id"));
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; 更新供应商提货计划支付单成功, 记录id: " + temp_id[0]);
//                    } else { // 返回结果行数为0
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; 没有更新供应商提货计划支付单");
//                    }
//                    log.info("更新供应商提货计划支付单成功, 更新行数: " + ar4.result().rowCount());
//                    promise.complete(responseJSObj);
//                } else {
//                    log.info("更新供应商提货计划支付单失败, 失败原因: " + ar4.cause().getMessage());
//                    promise.fail(CommonUtils.createExceptionJsonObjectStr(ar4, "/web/supplier_pickup_plan_payment_detail/update", "服务器处理异常", update_sql3));
//                }
//            })
//            .compose(res6 -> res1.commit()))
//            .eventually(v -> conn.close()))
//            .onSuccess(v -> log.info("更新供应商提货计划支付明细单事务处理成功"))
//            .onFailure(err -> log.info("更新供应商提货计划支付明细单事务处理失败, 失败原因: " + err.getMessage())))));
//
//        }else {
//        	// 事务开始
//            conn.begin()
//            .compose(res1 -> conn.preparedQuery(update_sql1).executeBatch(update_supplier_pickup_plan_payment_detail_batch).onComplete(ar1 -> {
//                if (ar1.succeeded()) {
//                    if (ar1.result().rowCount() > 0) {
//                        // 取出记录的id
//                        long[] temp_id = new long[1];
//                        ar1.result().forEach(item -> temp_id[0] = item.getLong("id"));
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "更新供应商提货计划支付明细单成功, 记录id: " + temp_id[0]);
//                    } else { // 返回结果行数为0
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "没有更新供应商提货计划支付明细单");
//                    }
//                    log.info("更新供应商提货计划支付明细单成功, 更新行数: " + ar1.result().rowCount());
//                } else {
//                    log.info("更新供应商提货计划支付明细单失败, 失败原因: " + ar1.cause().getMessage());
//                    promise.fail(CommonUtils.createExceptionJsonObjectStr(ar1, "/web/supplier_pickup_plan_payment_detail/update", "服务器处理异常", update_sql1));
//                }
//            })
//            .compose(res3 -> conn.preparedQuery(select_sum_sql).execute(Tuple.of(supplier_pickup_plan_detail_id)).onComplete(ar3 -> {
//                if (ar3.succeeded()) {
//                    ar3.result().forEach(item -> {
//                        payment_total_weight[0] = item.getDouble("payment_total_weight");
//                        real_pickup_total_number[0] = item.getDouble("real_pickup_total_number");
//                        real_payment_total_amount[0] = item.getDouble("real_payment_total_amount");
//                    });
//                    log.info("供应商提货计划支付明细单求和成功");
//                } else {
//                    log.info("供应商提货计划支付明细单求和失败, 失败原因: " + ar3.cause().getMessage());
//                    promise.fail(CommonUtils.createExceptionJsonObjectStr(ar3, "/web/supplier_pickup_plan_payment_detail/update", "服务器处理异常", select_sum_sql));
//                }
//            })
//            .compose(res4 -> conn.preparedQuery(update_sql2).execute(Tuple.of(payment_total_weight[0], real_pickup_total_number[0],
//            real_payment_total_amount[0], user_id, supplier_pickup_plan_detail_id)).onComplete(ar4 -> {
//                if (ar4.succeeded()) {
//                    if (ar4.result().rowCount() > 0) {
//                        // 取出新增记录的id
//                        List<Long> ids = new ArrayList<>(ar4.result().rowCount());
//                        ar4.result().forEach(item -> ids.add(item.getLong("id")));
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; 新增供应商提货计划支付明细单成功, 记录id: " + ids);
//                    } else { // 返回结果行数为0
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; 没有新增供应商提货计划支付明细单");
//                    }
//                    log.info("新增供应商提货计划支付明细单成功, 新增行数: " + ar4.result().rowCount());
//                } else {
//                    log.info("新增供应商提货计划支付明细单失败, 失败原因: " + ar4.cause().getMessage());
//                    promise.fail(CommonUtils.createExceptionJsonObjectStr(ar4, "/web/supplier_pickup_plan_payment_detail/update", "服务器处理异常", update_sql2));
//                }
//            })
//            .compose(res5 -> conn.preparedQuery(update_sql3).execute(Tuple.of(supplier_pickup_plan_payment_id, user_id, supplier_pickup_plan_payment_id)).onComplete(ar4 -> {
//                if (ar4.succeeded()) {
//                    if (ar4.result().rowCount() > 0) {
//                        // 取出记录的id
//                        long[] temp_id = new long[1];
//                        ar4.result().forEach(item -> temp_id[0] = item.getLong("id"));
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; 更新供应商提货计划支付单成功, 记录id: " + temp_id[0]);
//                    } else { // 返回结果行数为0
//                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; 没有更新供应商提货计划支付单");
//                    }
//                    log.info("更新供应商提货计划支付单成功, 更新行数: " + ar4.result().rowCount());
//                    promise.complete(responseJSObj);
//                } else {
//                    log.info("更新供应商提货计划支付单失败, 失败原因: " + ar4.cause().getMessage());
//                    promise.fail(CommonUtils.createExceptionJsonObjectStr(ar4, "/web/supplier_pickup_plan_payment_detail/update", "服务器处理异常", update_sql3));
//                }
//            })
//            .compose(res6 -> res1.commit()))
//            .eventually(v -> conn.close()))
//            .onSuccess(v -> log.info("更新供应商提货计划支付明细单事务处理成功"))
//            .onFailure(err -> log.info("更新供应商提货计划支付明细单事务处理失败, 失败原因: " + err.getMessage()))));
//
//        }
//
//        return promise.future();
//    }
//}